<?php
namespace App\Middleware\Auth;

use App\Constants\Constant;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Exception\UnauthorizedHttpException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Qbhy\HyperfAuth\AuthManager;

class AdminMiddleware implements MiddlewareInterface {

    /**
     * @Inject
     * @var AuthManager
     */
    protected $auth;

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $guard = $this->auth->guard(Constant::ADMIN);

        if ($guard->guest()) {
            throw new UnauthorizedHttpException("Without authorization from {$guard->getName()} guard");
        }

        return $handler->handle($request);
    }
}